﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using qna.Business;
using qna.Controls;
using System.Diagnostics;
using qna.Services;

namespace qna.pages {
	public partial class Questions : System.Web.UI.Page {
		protected void Page_Load(object sender, EventArgs e) {
			CreateList();
			userId.Value = User.Identity.Name;
		}

		private void CreateList() {
			QuestionService service = new Services.QuestionService();
			IList<int> subjects = new List<int>();
			int limit = Convert.ToInt32(Request.QueryString["limit"]);
			int rating = Convert.ToInt32(Request.QueryString["Rating"]);

			//get subjects from query string
			string raw = Request.QueryString["subjects"];
			if (raw != "" && raw != null) {
				foreach (string s in raw.Split(',')) {
					subjects.Add(int.Parse(s));
				}
			}

			//retrieve questions
			int[] subs = subjects.ToArray();
			string from = Request.QueryString["from"];
			Business.Question[] qs = limit == 0 ? service.List(subs, from, rating) : service.List(subs, from, limit, rating);
			//list questions
			foreach (Business.Question q in qs) {
				QuestionControl qc = (QuestionControl)LoadControl("~/Controls/QuestionControl.ascx");
				qc.Question = q;
				QuestionList.Controls.Add(qc);
			}
		}
	}
}